package com.angel.kuaidi;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.angel.kuaidi.db.DB;
import com.angel.kuaidi.entity.WuliuCompanyInfo;
import com.angel.kuaidi.service.RemotePictureManager;
import com.angel.kuaidi.utils.SharePreferenceManager;
import com.angel.kuaidi.utils.StringUtils;
import com.angel.kuaidi.utils.Utils;
import com.angel.kuaidi.utils.UtilsLog;

public class KuaidiApp extends Application {

	private static KuaidiApp mApp;

	// private UserInfo mUserInfo;
	// private boolean downloading = false;
	private SharePreferenceManager spm = null;
	private RemotePictureManager rpm = null;
	private static DB db;
	private WuliuCompanyInfo company;

	private static boolean isSpend = true;

	@Override
	public void onCreate() {
		super.onCreate();
		mApp = (KuaidiApp) getApplicationContext();
		UtilsLog.i("msg", "app init");
		// 启动统计
		// new Analytics(this);
		toastMgr.builder.init(mApp);
		/** 如果应用没有清理缓存功能，则在应用启动时清理缓存 */
		db = DB.getInstance(mApp);

	}

	public WuliuCompanyInfo getCompany() {
		return company;
	}

	public void setCompany(WuliuCompanyInfo company) {
		this.company = company;
	}

	public static boolean getIsSpend() {
		return isSpend;
	}

	public static void setIsSpend(boolean b) {
		isSpend = b;
	}

	public static DB getDB() {

		if (db == null) {
			db = DB.getInstance(getSelf());
		}
		return db;
	}

	public static KuaidiApp getSelf() {

		return mApp;
	}

	public SharePreferenceManager getShareManager() {

		if (spm == null) {
			spm = new SharePreferenceManager();
		}
		return spm;
	}

	public RemotePictureManager getRPM() {

		if (rpm == null) {
			rpm = new RemotePictureManager(getSelf());
		}
		return rpm;
	}

	/**
	 * toast singleton，用来统一显示toast，这样就可以实现toast的快速刷新
	 * 
	 */
	public enum toastMgr {
		builder;

		private View v;
		private TextView tv;
		private Toast toast;

		private void init(Context c) {
			//v = Toast.makeText(c, "", Toast.LENGTH_SHORT).getView();
			v = LayoutInflater.from(c).inflate(R.layout.toast, null);
			tv = (TextView) v.findViewById(R.id.tv_toast);
			toast = new Toast(c);
			toast.setView(v);
		}

		public void display(CharSequence text, int duration) {
			if (text.length() != 0) {
				tv.setText(text);
				toast.setDuration(duration);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}

		public void display(int Resid, int duration) {
			if (Resid != 0) {
				tv.setText(Resid);
				toast.setDuration(duration);
				toast.show();
			}
		}
	}
}
